package com.oracle.lingshangejia.model.base;



import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.oracle.lingshangejia.dialog.LoadingViewDialog;


public abstract class BaseFragment extends Fragment {
    public View view;
    protected Context mContext;
    private LoadingViewDialog mLoadingView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext=context;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mContext=null;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (view!=null){
            ViewGroup viewGroup= (ViewGroup) view.getParent();
            if (viewGroup!=null)
                viewGroup.removeView(view);
        }else{
            view=inflater.inflate(getLayoutId(),container,false);
            onBindViewBefore(view);

            if (savedInstanceState!=null){
                onRestartInstance(savedInstanceState);
            }

            initView();
            initData();
        }

        return view;


    }

    protected  abstract void initData();

    ;

    protected  abstract void initView();

    protected  void onRestartInstance(Bundle savedInstanceState){

    };

    protected  abstract void onBindViewBefore(View view);

    protected abstract int getLayoutId();
    public void showLoading(){
        mLoadingView = new LoadingViewDialog();
        mLoadingView.show(getFragmentManager(),"" );
    }
    public void dismissLoading(){
        if (mLoadingView != null){
            mLoadingView.dismiss();
        }

    }
}
